﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ListenSetting : MonoBehaviour
{
    public InputField Host,Port;
    public Button startedBtn;
    public Text startedBtnText;
    private void Awake()
    {
        startedBtn.onClick.AddListener(OnStartedButtonClick);
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        switch (Singleton<TcpServer>.GetInstance().CurrentState)
        {
            case ServerState.Unstarted:
                startedBtnText.text = "服务未启动(点击启动)";
                    break;
            case ServerState.Started:
                startedBtnText.text = "服务已启动";
                break;
            case ServerState.Stopped:
                startedBtnText.text = "服务已暂停(点击恢复)";
                break;
            case ServerState.WaitRcving:
                startedBtnText.text = "服务运行中(点击暂停)";
                break;
            default:
                break;
        }

    }
    void OnStartedButtonClick()
    {
        if (Singleton<TcpServer>.GetInstance().CurrentState == ServerState.Unstarted)
        {
            Singleton<TcpServer>.GetInstance().Host = Host.text.Trim();
            Singleton<TcpServer>.GetInstance().Port = Convert.ToInt32(Port.text.Trim());
            Singleton<TcpServer>.GetInstance().Start();
        }
        else if (Singleton<TcpServer>.GetInstance().CurrentState == ServerState.Stopped)
            Singleton<TcpServer>.GetInstance().Resume();

        else if (Singleton<TcpServer>.GetInstance().CurrentState == ServerState.WaitRcving)
            Singleton<TcpServer>.GetInstance().Pause();
        else
            return;
    }
}
